package com.ibeifeng.sparkwar2.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.ibeifeng.sparkwar2.dao.*;
import com.ibeifeng.sparkwar2.service.FindAllService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther:搬砖小能手
 * @Date:2021/5/10
 * @Description:com.ibeifeng.sparkwar2.controller
 * @version:1.0
 */
@Controller
public class MyController {
    @Autowired
    FindAllService findAllService;



    @GetMapping("/session")
    public  String  getSession(Model model){

        List<SessionAggrStat> sessionAggrStats= findAllService.findSessionAggrStat();


        SessionAggrStat sessionAggrStat=sessionAggrStats.get(0);

        model.addAttribute("task_id",sessionAggrStat.getTaskid());
        model.addAttribute("session_count",sessionAggrStat.getSession_count());
        model.addAttribute("visit_length_1s_3s_ratio",sessionAggrStat.getStep_length_1_3_ratio());

        model.addAttribute("visit_length_4s_6s_ratio",sessionAggrStat.getVisit_length_4s_6s_ratio());
        model.addAttribute("visit_length_7s_9s_ratio",sessionAggrStat.getStep_length_7_9_ratio());
        model.addAttribute("visit_length_10s_30s_ratio",sessionAggrStat.getStep_length_10_30_ratio());
        model.addAttribute("visit_length_30s_60s_ratio",sessionAggrStat.getStep_length_30_60_ratio());
        model.addAttribute("visit_length_1m_3m_ratio",sessionAggrStat.getStep_length_1_3_ratio());
        model.addAttribute("visit_length_10m_30m_ratio",sessionAggrStat.getStep_length_10_30_ratio());
        model.addAttribute("visit_length_30m_ratio",sessionAggrStat.getVisit_length_30m_ratio());


        model.addAttribute("step_length_1_3_ratio",sessionAggrStat.getStep_length_1_3_ratio());

        model.addAttribute("step_length_4_6_ratio",sessionAggrStat.getStep_length_4_6_ratio());
        model.addAttribute("step_length_7_9_ratio",sessionAggrStat.getStep_length_7_9_ratio());
        model.addAttribute("step_length_10_30_ratio",sessionAggrStat.getStep_length_10_30_ratio());
        model.addAttribute("step_length_30_60_ratio",sessionAggrStat.getStep_length_30_60_ratio());
        model.addAttribute("step_length_60_ratio",sessionAggrStat.getStep_length_60_ratio());


        return "index";

    }
    @GetMapping("/detail")
    public  String Session_detail(Model model){
        List<SessionDetail> sessionDetails = findAllService.findSessionDetail();
        System.out.println(sessionDetails.size()+"   SessionDetail");
        for(SessionDetail session :sessionDetails)
        {
            System.out.println(session.toString());

        }
        model.addAttribute("sessionDetails",sessionDetails);


        return "session_detail";
    }
    @GetMapping("/random")
    public  String Session_random(Model model){
        List<SessionRandomExtract> sessionRandomExtracts = findAllService.findSessionRandomExtract();

        System.out.println(sessionRandomExtracts.size()+"   SessionDetail");
        for(SessionRandomExtract session :sessionRandomExtracts)
        {
            System.out.println(session.toString());

        }
        model.addAttribute("sessionRandomExtracts",sessionRandomExtracts);


        return "session_random";
    }

    @GetMapping("/top10")
    @ResponseBody
    public  String TOP10(Model model){
        //取出top10
        List<Top10Category> top10Categorys = findAllService.findTop10Category();
        String  top10= JSON.toJSONString(top10Categorys);
        System.out.println(top10);

        return top10;
    }

    @GetMapping("/top10sesssion")
    public  String top10session(Model model){
        List<Top10Session> top10Sessions = findAllService.findTop10Session();

        System.out.println(top10Sessions.size()+"   SessionDetail");
        for(Top10Session session :top10Sessions)
        {
            System.out.println(session.toString());

        }
        model.addAttribute("top10Sessions",top10Sessions);


        return "session_top10";
    }

    @GetMapping("/convertRate")
    @ResponseBody
    public  String convertRate(Model model){
        PageSplitConvertRate pageSplitConvertRate = findAllService.findPageSplitConvertRate();
        String convert_rate = pageSplitConvertRate.getConvert_rate();
        String[] strArr = convert_rate.split("\\|");


        Arrays.sort(strArr);
      for(int i=0;i<strArr.length;i++){
          strArr[i]="\"page"+strArr[i];
      }
        String s = Arrays.toString(strArr);

        String s1 = s.replace("[", "{");
        String s2 = s1.replace("]", "}");
        String s3 = s2.replace("=", ":");
        String s4 = s3.replaceAll("_", "_page");
        String s5 = s4.replaceAll(":", "\":");
        System.out.println(s5);



        return s5;
    }
    @GetMapping("/top3product")
    public  String top3product(Model model){
        List<AreaTop3Product> areaTop3Product = findAllService.findAreaTop3Product();
        model.addAttribute("areaTop3Product",areaTop3Product);


        return "areaTop3Product";
    }



}
